<?
/***************************************************************************
*                            Dolphin Smart Community Builder
*                              -------------------
*     begin                : Mon Mar 23 2006
*     copyright            : (C) 2007 BoonEx Location
*     website              : http://www.boonex.com
* This file is part of Dolphin - Smart Community Builder
*
* Dolphin is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the
* License, or  any later version.
*
* Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Dolphin,
* see license.txt file; if not, write to marketing@boonex.com
***************************************************************************/

bx_import('BxDolTwigModuleDb');

/*
 * Location module Data
 */
class BxLocationDb extends BxDolTwigModuleDb {	
	
	var $_oConfig;

	/*
	 * Constructor.
	 */
	function BxLocationDb(&$oConfig) {
        parent::BxDolTwigModuleDb($oConfig);

		$this->_oConfig = $oConfig;

		/********Event ************/
        $this->_sTableEvent = 'event_main';
        $this->_sEventPrefix = 'modzzz_location_event';
        $this->_sTableEventMediaPrefix = 'event_';


		/********School ************/
        $this->_sTableSchool = 'school_main';
        $this->_sSchoolPrefix = 'modzzz_location_school';
        $this->_sTableSchoolMediaPrefix = 'school_';

		/********Service ************/
        $this->_sTableService = 'service_main';
        $this->_sServicePrefix = 'modzzz_location_service';
        $this->_sTableServiceMediaPrefix = 'service_';

		/********Shop ************/
        $this->_sTableShop = 'shop_main';
        $this->_sShopPrefix = 'modzzz_location_shop';
        $this->_sTableShopMediaPrefix = 'shop_';

		/********Nightlife ************/
        $this->_sTableNightlife = 'nightlife_main';
        $this->_sNightlifePrefix = 'modzzz_location_nightlife';
        $this->_sTableNightlifeMediaPrefix = 'nightlife_';

		/********Transport ************/
        $this->_sTableTransport = 'transport_main';
        $this->_sTransportPrefix = 'modzzz_location_transport';
        $this->_sTableTransportMediaPrefix = 'transport_';

		/********Hotel ************/
        $this->_sTableHotel = 'hotel_main';
        $this->_sHotelPrefix = 'modzzz_location_hotel';
        $this->_sTableHotelMediaPrefix = 'hotel_';

		/********Food ************/
        $this->_sTableFood = 'food_main';
        $this->_sFoodPrefix = 'modzzz_location_food';
        $this->_sTableFoodMediaPrefix = 'food_';

        $this->_sTableProfile = 'profiles';
        $this->_sTableSubProfile = 'sub_profiles';

        $this->_sTableMain = 'main';
        $this->_sTableMediaPrefix = '';
        $this->_sFieldId = 'id';
        $this->_sFieldAuthorId = 'author_id';
        $this->_sFieldUri = 'uri';
        $this->_sFieldTitle = 'title';
        $this->_sFieldDescription = 'desc';
        $this->_sFieldTags = 'tags';
        $this->_sFieldThumb = 'thumb';
        $this->_sFieldStatus = 'status';
        $this->_sFieldFeatured = 'featured';
        $this->_sFieldCreated = 'created';
        $this->_sTableAdmins = 'admins';
        $this->_sFieldAllowViewTo = 'allow_view_to';
	}
  
	function setItemStatus($iItemId, $sStatus) {
		
 		 $this->query("UPDATE `" . $this->_sPrefix . "main` SET `status`='$sStatus' WHERE `id`='$iItemId'"); 
	}
 
 	function getAdmins() { 
	
 		$aAllAdmins = $this->getAll("SELECT `ID` FROM `Profiles` WHERE `Role` & " . BX_DOL_ROLE_ADMIN . " OR `Role` & " . BX_DOL_ROLE_MODERATOR . " AND `status`='Active'"); 
		
		return $aAllAdmins; 
	}

 	function isOwnerAdmin($iProfileId) {
	 
 		$bAdmin = $this->getOne("SELECT `ID` FROM `Profiles` WHERE `ID`='$iProfileId' AND (`Role` & " . BX_DOL_ROLE_ADMIN . " OR `Role` & " . BX_DOL_ROLE_MODERATOR . ") AND `status`='Active'"); 
		
		return $bAdmin; 
	}
 
    function deleteEntryByIdAndOwner ($iId, $iOwner, $isAdmin) {
        if ($iRet = parent::deleteEntryByIdAndOwner ($iId, $iOwner, $isAdmin)) {
            
            $this->query ("DELETE FROM `" . $this->_sPrefix . "fans` WHERE `id_entry` = $iId");
			
			$this->query ("DELETE FROM `" . $this->_sPrefix . "admins` WHERE `id_entry` = $iId");
            $this->deleteEntryMediaAll ($iId, 'images');
            $this->deleteEntryMediaAll ($iId, 'videos');
            $this->deleteEntryMediaAll ($iId, 'sounds');
            $this->deleteEntryMediaAll ($iId, 'files');  
			
			$this->deleteLocation ($iId);

 			$this->query("DELETE FROM `" . $this->_sPrefix . "review` WHERE `location_id`=$iId"); 
 			$this->query("DELETE FROM `" . $this->_sPrefix . $this->_sTableNightlife . "` WHERE `location_id`=$iId");
 			$this->query("DELETE FROM `" . $this->_sPrefix . $this->_sTableShop . "` WHERE `location_id`=$iId");
 			$this->query("DELETE FROM `" . $this->_sPrefix . $this->_sTableSchool . "` WHERE `location_id`=$iId");
 			$this->query("DELETE FROM `" . $this->_sPrefix . $this->_sTableService . "` WHERE `location_id`=$iId");
 			$this->query("DELETE FROM `" . $this->_sPrefix . $this->_sTableHotel . "` WHERE `location_id`=$iId");
 			$this->query("DELETE FROM `" . $this->_sPrefix . $this->_sTableEvent . "` WHERE `location_id`=$iId");
 			$this->query("DELETE FROM `" . $this->_sPrefix . $this->_sTableTransport . "` WHERE `location_id`=$iId");
 			$this->query("DELETE FROM `" . $this->_sPrefix . $this->_sTableFood . "` WHERE `location_id`=$iId"); 
        }

        return $iRet;
    }
 
	function getNumberList($iStart, $iEnd, $iIncrement=1) {
	
		$aVals=array();
		$aVals[0] = ''; 
		for($iter=$iStart; $iter<=$iEnd; $iter+=$iIncrement) { 
			$aVals[$iter] = $iter; 
		}

		return $aVals;
	}

	function getYearList($iNumYears) {
	
		$iNow = date("Y");
		$aVals=array();
		$aVals[0] = ''; 
		for($iter=$iNow; $iter>=($iNow-$iNumYears); $iter--)
		{
			$aVals[$iter] = $iter; 
		}
  
		return $aVals;
	}

	/*[begin] map*/
    function updateLocation ($iAuthorId, $iId, $fLat, $fLng, $iZoom, $iType) {
        return $this->query ("INSERT INTO `" . $this->_sPrefix . "profiles` SET `id` = '$iId', `author_id` = '$iAuthorId', `ts` = UNIX_TIMESTAMP(), `lat` = '$fLat', `lng` = '$fLng', `zoom` = '$iZoom', `type` = '$iType' ON DUPLICATE KEY UPDATE `ts` = UNIX_TIMESTAMP(), `lat` = '$fLat', `lng` = '$fLng', `zoom` = '$iZoom', `type` = '$iType'");
    }

    function deleteLocation ($iId) {
        return $this->query ("DELETE FROM `" . $this->_sPrefix . "profiles` WHERE `id` = '$iId'");
    }

    function insertCountryLocation ($sCountryCode, $fLat, $fLng, $isFailed = 0) {
        return $this->query ("INSERT INTO `" . $this->_sPrefix . "countries` SET `country` = '$sCountryCode', `lat` = '$fLat', `lng` = '$fLng', `failed` = '$isFailed' ON DUPLICATE KEY UPDATE `lat` = '$fLat', `lng` = '$fLng', `failed` = '$isFailed'");
    }

    function insertCityLocation ($sCountryCode, $sCity, $fLat, $fLng, $isFailed = 0) {
        return $this->query ("INSERT INTO `" . $this->_sPrefix . "cities` SET `country` = '$sCountryCode', `city` = '$sCity', `lat` = '$fLat', `lng` = '$fLng', `failed` = '$isFailed' ON DUPLICATE KEY UPDATE `lat` = '$fLat', `lng` = '$fLng', `failed` = '$isFailed'");
    }

    function insertProfileLocation ($iAuthorId, $iId, $fLat, $fLng, $iMapZoom, $sMapType, $sAddress, $sCountry, $iPrivacy = 0, $isFailed = 0) {
        $sPrivacyUpdate = '';
        $sPrivacyInsert = "`allow_view_location_to` = '" . BX_LOCATION_DEFAULT_PRIVACY . "',";
        if ($iPrivacy) {
            $sPrivacyInsert = $sPrivacyUpdate = "`allow_view_location_to` = '$iPrivacy',";
        }
        return $this->query ("INSERT INTO `" . $this->_sPrefix . "profiles` SET `id` = '$iId', `author_id` = '$iAuthorId', `lat` = '$fLat', `lng` = '$fLng', `zoom` = '$iMapZoom', `type` = '$sMapType', `address` = '$sAddress', `country`= '$sCountry', $sPrivacyInsert `ts` = UNIX_TIMESTAMP(), `failed` = '$isFailed' ON DUPLICATE KEY UPDATE  `lat` = '$fLat', `lng` = '$fLng', `zoom` = '$iMapZoom', `type` = '$sMapType', `address` = '$sAddress', `country`= '$sCountry', $sPrivacyUpdate `ts` = UNIX_TIMESTAMP(), `failed` = '$isFailed'");
    }

    function getUndefinedCountries ($iLimit) {
        return $this->getPairs ("SELECT `c`.`ISO2`, `c`.`country` FROM `sys_countries` AS `c` LEFT JOIN `" . $this->_sPrefix . "countries` AS `m` ON (`m`.`country` = `c`.`ISO2`) WHERE ISNULL(`m`.`country`) LIMIT $iLimit", 'ISO2', 'Country');
    }

    function getUndefinedCities ($iLimit) {
        return $this->getPairs ("SELECT `p`.`city`, `p`.`country` FROM `" . $this->_sPrefix . "main` AS `p` LEFT JOIN `" . $this->_sPrefix . "cities` AS `m` ON (`m`.`country` = `p`.`country` AND `m`.`city` = `p`.`city`) WHERE ISNULL(`m`.`country`) LIMIT $iLimit", 'country', 'city');
    }    

    function getUndefinedProfiles ($iLimit) {
        return $this->getAllWithKey ("SELECT `p`.* FROM `" . $this->_sPrefix . "main` AS `p` LEFT JOIN `" . $this->_sPrefix . "profiles` AS `m` ON (`m`.`id` = `p`.`id`) WHERE ISNULL(`m`.`id`) LIMIT $iLimit", 'id');
    }    

    function getProfileInfo ($iID) {
        return $this->getRow("SELECT * FROM `" . $this->_sPrefix . "main` WHERE `id`='$iID'");
    }    
 
    function clearProfiles ($isClearFailedOnly) {
        return $this->_clearTable ($isClearFailedOnly, 'profiles');
    }

    function clearCountries ($isClearFailedOnly) {
        return $this->_clearTable ($isClearFailedOnly, 'countries');
    }

    function clearCities ($isClearFailedOnly) {
        return $this->_clearTable ($isClearFailedOnly, 'cities');
    }

    function _clearTable ($isClearFailedOnly, $sTable) {
        if ($isClearFailedOnly) {
            $ret = $this->query ("DELETE FROM `" . $this->_sPrefix . "$sTable` WHERE `failed` != 0");
            $this->query ("OPTIMIZE TABLE `" . $this->_sPrefix . "$sTable`");
            return $ret;
        } else {
            return $this->query ("TRUNCATE TABLE `" . $this->_sPrefix . "$sTable`");
        }
    }    

    function getProfileById($iProfileId) { 
        return $this->getRow("SELECT `m`.`id`, `p`.`title`, `p`.`address1`, `p`.`address2`, `p`.`thumb`, `m`.`lat`, `m`.`lng`, `m`.`zoom`, `m`.`type`, `m`.`address`, `m`.`country`, `p`.`city`, `m`.`allow_view_location_to` FROM `" . $this->_sPrefix . "profiles` AS `m` INNER JOIN `" . $this->_sPrefix . "main` AS `p` ON (`p`.`id` = `m`.`id`) WHERE `m`.`failed` = 0 AND `p`.`status` = 'approved' AND `m`.`id` = '$iProfileId' LIMIT 1");
    } 

    function getAuthorById($iProfileId) { 
        return $this->getRow("SELECT `author_id` FROM `" . $this->_sPrefix . "main` WHERE `id` = '$iProfileId' LIMIT 1");
    } 


    function getProfilesByBounds($fLatMin, $fLatMax, $fLngMin, $fLngMax) {
        $sWhere = $this->_getLatLngWhere ($fLatMin, $fLatMax, $fLngMin, $fLngMax);
        return $this->getAll("SELECT `m`.`id`, `p`.`thumb`, `p`.`title` AS `NickName`, `m`.`lat`, `m`.`lng` FROM `" . $this->_sPrefix . "profiles` AS `m` INNER JOIN `" . $this->_sPrefix . "main` AS `p` ON (`p`.`id` = `m`.`id`) WHERE `m`.`failed` = 0 AND `p`.`status` = 'approved' AND `m`.`allow_view_location_to` = '" . BX_DOL_PG_ALL . "' $sWhere LIMIT 100");
    } 

    function getCountryByCode($sCountryCode, $isScrict = true) {

        $sJoin = $isScrict ? 'INNER' : 'LEFT';

        return $this->getRow("SELECT `m`.`country`, `m`.`lat`, `m`.`lng`, COUNT(`p`.`id`) AS `num`
            FROM `" . $this->_sPrefix . "countries` AS `m` 
            $sJoin JOIN `" . $this->_sPrefix . "main` AS `p` ON (`p`.`country` = `m`.`country` AND `p`.`status` = 'approved') 
            $sJoin JOIN `" . $this->_sPrefix . "profiles` AS `pm` ON (`pm`.`id` = `p`.`id` AND `pm`.`failed` = 0 AND `pm`.`allow_view_location_to` = '" . BX_DOL_PG_ALL . "')
            WHERE `m`.`failed` = 0 AND `m`.`country` = '$sCountryCode'
            GROUP BY `p`.`country`
            LIMIT 1"); 
    } 

    function getCountriesByBounds($fLatMin, $fLatMax, $fLngMin, $fLngMax) {
        $sWhere = $this->_getLatLngWhere ($fLatMin, $fLatMax, $fLngMin, $fLngMax);
        return $this->getAll("SELECT `m`.`country`, `m`.`lat`, `m`.`lng`, COUNT(`p`.`id`) AS `num`
            FROM `" . $this->_sPrefix . "countries` AS `m` 
            INNER JOIN `" . $this->_sPrefix . "main` AS `p` ON (`p`.`country` = `m`.`country` AND `p`.`status` = 'approved') 
            INNER JOIN `" . $this->_sPrefix . "profiles` AS `pm` ON (`pm`.`id` = `p`.`id` AND `pm`.`failed` = 0 AND `pm`.`allow_view_location_to` = '" . BX_DOL_PG_ALL . "')
            WHERE `m`.`failed` = 0 $sWhere 
            GROUP BY `p`.`country`
            LIMIT 100"); 
    } 

    function getCitiesByBounds($fLatMin, $fLatMax, $fLngMin, $fLngMax) {
        $sWhere = $this->_getLatLngWhere ($fLatMin, $fLatMax, $fLngMin, $fLngMax);
        return $this->getAll("SELECT `m`.`country`, `m`.`city`, `m`.`lat`, `m`.`lng`, COUNT(`p`.`id`) AS `num`
            FROM `" . $this->_sPrefix . "cities` AS `m`
            INNER JOIN `" . $this->_sPrefix . "main` AS `p` ON (`p`.`country` = `m`.`country` AND `p`.`city` = `m`.`city` AND `p`.`status` = 'approved')
            INNER JOIN `" . $this->_sPrefix . "profiles` AS `pm` ON (`pm`.`id` = `p`.`id` AND `pm`.`failed` = 0 AND `pm`.`allow_view_location_to` = '" . BX_DOL_PG_ALL . "')
            WHERE `m`.`failed` = 0 $sWhere 
            GROUP BY `m`.`city`
            LIMIT 100"); 
    } 

    function _getLatLngWhere ($fLatMin, $fLatMax, $fLngMin, $fLngMax) {

        $sWhere = " AND `m`.`lat` < $fLatMax AND `m`.`lat` > $fLatMin ";
        if ($fLngMin < $fLngMax)
            $sWhere .= " AND `m`.`lng` < $fLngMax AND `m`.`lng` > $fLngMin ";
        else
            $sWhere .= " AND ((`m`.`lng` < $fLngMax AND `m`.`lng` > -180) OR (`m`.`lng` < 180 AND `m`.`lng` > $fLngMin)) ";
        return $sWhere;
    }    

    function isCityLocationExists($sCountryCode, $sCity) {
        return $this->getOne("SELECT `m`.`country` FROM `" . $this->_sPrefix . "cities` AS `m` WHERE `m`.`country` = '$sCountryCode' AND `m`.`city` = '$sCity' AND `m`.`failed` = 0 LIMIT 1") ? true : false;
    }

    function getSettingsCategory($s) {
        return $this->getOne("SELECT `ID` FROM `sys_options_cats` WHERE `name` = '$s' LIMIT 1");
    }   

	/*[end] map*/
 
  
 
   /** category functions*/
	function getCategoryName($iId){
		return $this->getOne("SELECT `name` FROM `" . $this->_sPrefix . "categ` WHERE `id` = '$iId' LIMIT 1"); 
	}

	function getParentCategories(){
		return $this->getAll("SELECT `id`, `parent`, `name`, `uri` FROM `" . $this->_sPrefix . "categ` WHERE `parent` = 0  ORDER BY `name` ASC"); 
	}

 	function getSubCategories($iId){
		return $this->getAll("SELECT `id`, `parent`, `name`, `uri`  FROM `" . $this->_sPrefix . "categ` WHERE `parent` = '$iId' ORDER BY `name` ASC"); 
	}

	function getCategoryInfo ($iId=0, $bGetAll=false){
		if($bGetAll){
			return $this->getAll("SELECT `id`, `name`, `uri` FROM `" . $this->_sPrefix . "categ` WHERE `active` = 1 AND `parent`=0");
		}else{
			return $this->getRow("SELECT `id`, `name`, `uri` FROM `" . $this->_sPrefix . "categ` WHERE `id` = '$iId'");
		}
	}
 
	function getParentCategoryById($iId){
		return $this->getOne("SELECT `parent` FROM `" . $this->_sPrefix . "categ` WHERE `id` = '$iId'"); 
	}

	function getParentCategoryByUri($sUri){
		return $this->getOne("SELECT `parent` FROM `" . $this->_sPrefix . "categ` WHERE `uri` = '$sUri'"); 
	}

	function getCategoryUriById($iId){
		return $this->getOne("SELECT `uri` FROM `" . $this->_sPrefix . "categ` WHERE `id` = '$iId'"); 
	}

	function getCategoryIdByUri($sUri){
		return $this->getOne("SELECT `id` FROM `" . $this->_sPrefix . "categ` WHERE `uri` = '$sUri'"); 
	}

	function getCategoryById($iId){
		return $this->getRow("SELECT `id`,`name`,`uri` FROM `" . $this->_sPrefix . "categ` WHERE `id` = '$iId'"); 
	}

	function getCategoryByUri($sUri){
		return $this->getRow("SELECT `id`,`name`,`uri` FROM `" . $this->_sPrefix . "categ` WHERE `uri` = '$sUri'"); 
	}
 
	function getParentCategoryInfo ($iId){
		$iParentId = (int)$this->getOne("SELECT `parent` FROM `" . $this->_sPrefix . "categ` WHERE `id` = '$iId'");
 
		return $this->getRow("SELECT `id`,`name`,`uri` FROM `" . $this->_sPrefix . "categ` WHERE `id` = '$iParentId'"); 
	}

	function getSubCategoryInfo ($iParentId=0, $iId=0, $bGetAll=false){
		if($bGetAll){ 
			return $this->getAll("SELECT `id`, `name`, `uri` FROM `" . $this->_sPrefix . "categ` WHERE `active` = 1 AND `parent`='$iParentId'");
		}else{
			return $this->getRow("SELECT `id`, `name`, `uri` FROM `" . $this->_sPrefix . "categ` WHERE `id` = '$iId'");
		}
	}
 
	function getAjaxCategoryOptions($iParentId) {
		$aCats = $this->getSubCategoryInfo ($iParentId, 0, true);

		$sOptions = "<option value=''></option>";
		foreach($aCats as $aEachCat){
			$iId = $aEachCat['id'];
			$sName = $aEachCat['name'];
			$sOptions .= "<option value='{$iId}'>{$sName}</option>";
		}

		return $sOptions;
	}
 
	function getFormCategoryArray($iCategory=0) {
		
		if($iCategory){
			$aCats = $this->getSubCategoryInfo ($iCategory, 0, true);
		}else{
			$aCats = $this->getCategoryInfo (0, true);
		}
		
		$aFormatCats = array();
		$aFormatCats[0] = '';
		foreach($aCats as $aEachCat){
			$iId = $aEachCat['id'];
			$sName = $aEachCat['name'];
			$aFormatCats[$iId] = $sName;
		}

		return $aFormatCats;
	}

	function getCategoryInfoByUri ($sUri){ 
		return $this->getRow("SELECT `id`, `parent`, `name`, `uri` FROM `" . $this->_sPrefix . "categ` WHERE `active` = 1 AND `uri`='$sUri'"); 
	}
 
	function getCategoryUrl ($sUri){
		return BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'categories/'.$sUri;
	}

 	function getSubCategoryUrl ($sUri){
		return BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'subcategories/'.$sUri;
	}

	function getParentCategoryCount($iParent){
		
		if (!$GLOBALS['logged']['admin']){ 
			if ($GLOBALS['logged']['member']){ 
				$aProfile = getProfileInfo($_COOKIE['memberID']); 
				require_once(BX_DIRECTORY_PATH_INC . 'membership_levels.inc.php');
				$aMembershipInfo = getMemberMembershipInfo($_COOKIE['memberID']); 
				$iMembershipId = $aMembershipInfo['ID']; 

			    $sExtraCheck = " AND location.`membership_view_filter` IN ('', '$iMembershipId')"; 
			}else{
				$sExtraCheck = "AND location.`membership_view_filter`=''";
			}
		}
 
		return  $this->getOne("SELECT count(location.`id`) FROM `" . $this->_sPrefix . "categ` cat LEFT JOIN `" . $this->_sPrefix . "main` location ON cat.id=location.`category_id` WHERE cat.`active` = 1 AND cat.`parent`=$iParent AND location.`status`='approved' {$sExtraCheck}"); 
	}

	function getStateCount($sState){
		
		if (!$GLOBALS['logged']['admin']){ 
			if ($GLOBALS['logged']['member']){ 
				$aProfile = getProfileInfo($_COOKIE['memberID']); 
				require_once(BX_DIRECTORY_PATH_INC . 'membership_levels.inc.php');
				$aMembershipInfo = getMemberMembershipInfo($_COOKIE['memberID']); 
				$iMembershipId = $aMembershipInfo['ID']; 

			    $sExtraCheck = " AND `membership_view_filter` IN ('', '$iMembershipId')"; 
			}else{
				$sExtraCheck = "AND `membership_view_filter`=''";
			}
		}
 
		return  $this->getOne("SELECT COUNT(`id`) FROM `" . $this->_sPrefix . "main` WHERE  `state` = '$sState'  AND `status`='approved' {$sExtraCheck}"); 
	}
  
	function getCategoryCount($iCategoryId){

		if (!$GLOBALS['logged']['admin']){ 
			if ($GLOBALS['logged']['member']){ 
				$aProfile = getProfileInfo($_COOKIE['memberID']); 
				require_once(BX_DIRECTORY_PATH_INC . 'membership_levels.inc.php');
				$aMembershipInfo = getMemberMembershipInfo($_COOKIE['memberID']); 
				$iMembershipId = $aMembershipInfo['ID']; 

			    $sExtraCheck = " AND location.`membership_view_filter` IN ('', '$iMembershipId')"; 
			}else{
				$sExtraCheck = "AND location.`membership_view_filter`=''";
			}
		}

		return  $this->getOne("SELECT count(location.`id`) FROM `" . $this->_sPrefix . "categ` cat LEFT JOIN `" . $this->_sPrefix . "main` location ON cat.id=location.`category_id` WHERE cat.`active` = 1 AND cat.`id`=$iCategoryId AND location.`status`='approved' {$sExtraCheck}"); 
	}
 	
	function SaveCategory($iParent=0)
	{  
		$sCategory = process_db_input($_POST['catname']);

		if(!trim($sCategory)){
			return false;
		}
	 
		if($iParent){
			$sExtraAdd = "`parent`='$iParent',";
		}

		$sCatURI = uriGenerate ($sCategory,  $this->_sPrefix . "categ", 'name');

		db_res("INSERT INTO `" . $this->_sPrefix . "categ` SET $sExtraAdd `uri`='$sCatURI', `name`='$sCategory'");
	 
		return true;
	}
	  
	function UpdateCategory()
	{  
		$iId = process_db_input($_POST['id']);
		$sCategory = process_db_input($_POST['catname']);
	 
		if(!trim($sCategory)){
			return false;   
		}
	  
		return $this->query("UPDATE `" . $this->_sPrefix . "categ` SET `name`='$sCategory'  WHERE `id`=$iId");
	}
	 
	function DeleteCategory()
	{  
		$iId = process_db_input($_POST['id']);
	 
		return $this->query("DELETE FROM `" . $this->_sPrefix . "categ` WHERE `id`='$iId'"); 
	} 

	/** state functions*/

 	function getStateName($sCountry, $sState=''){
		$sState = $this->getOne("SELECT `State` FROM `States` WHERE `CountryCode`='{$sCountry}' AND `StateCode`='{$sState}' LIMIT 1");
 
		return $sState;
	}

	function getStateOptions($sCountry='') {
		$aStates = $this->getStateArray ($sCountry);
 
		foreach($aStates as $aEachCode=>$aEachState){ 
			$sOptions .= "<option value='{$aEachCode}'>{$aEachState}</option>";
		}

		return $sOptions;
	}

 	function getStateArray($sCountry=''){
 
		$aStates = array();
		$aDbStates = $this->getAll("SELECT * FROM `States` WHERE `CountryCode`='{$sCountry}'  ORDER BY `State` ");
		 
		$aStates[] = '';
		foreach ($aDbStates as $aEachState){
			$sState = $aEachState['State'];
			$sStateCode = $aEachState['StateCode'];
			
			$aStates[$sStateCode] = $sState;
  		} 
		return $aStates;
	}

  
	function DeleteReview($iLocationId, $iReviewId, $sType=''){
 
 		if($sType)
			$sTypeQuery = "`type`='$sType' AND ";

 		$bSuccess = $this->query("DELETE FROM `" . $this->_sPrefix . "review` WHERE {$sTypeQuery}  `location_id`='{$iLocationId}' AND `id`='{$iReviewId}'");
  
		return $bSuccess;
	}
 
    function getReviewsBrowse(&$aProfiles, $iEntryId, $iStart, $iMaxNum, $sType='location') {
        return $this->getBatchReviews($aProfiles, $iEntryId, $iStart, $iMaxNum, $sType);
    }

    function getBatchReviews(&$aProfiles, $iEntryId, $iStart, $iMaxNum, $sType='location') {
    
        $aProfiles = $this->getAll ("SELECT SQL_CALC_FOUND_ROWS `r`.* FROM `" . $this->_sPrefix . "review` r  WHERE `location_id`='{$iEntryId}' ORDER BY `r`.`created` DESC LIMIT $iStart, $iMaxNum");
        return $this->getOne("SELECT FOUND_ROWS()");
    }

    function getProfileReviews(&$aProfiles, $iProfileId, $iStart, $iMaxNum) {
    
        $aProfiles = $this->getAll ("SELECT SQL_CALC_FOUND_ROWS `r`.* FROM `" . $this->_sPrefix . "review` r  WHERE `member_id`='{$iProfileId}' ORDER BY `r`.`created` DESC LIMIT $iStart, $iMaxNum");
        return $this->getOne("SELECT FOUND_ROWS()");
    }

	function getReviews($iEntryId=0, $sqlLimit='', $sType='') {
		$iEntryId = (int)$iEntryId; 
		if($iEntryId){
			if($sType=='' || $sType=='location')
				$sIdQuery = ' AND `location_id`='.$iEntryId;
			else
				$sIdQuery = ' AND `sub_id`='.$iEntryId;
		}

		if($sType)
			$sTypeQuery = " AND `type`='$sType'";

		$aAllEntries = $this->getAll("SELECT * FROM `" . $this->_sPrefix . "review` WHERE 1 {$sTypeQuery} {$sIdQuery} ORDER BY `created` DESC {$sqlLimit}");

		return $aAllEntries;   
	}

 	function getReviewCount($iEntryId=0, $sType='') {
		$iEntryId = (int)$iEntryId;
		if($iEntryId){
			if($sType=='' || $sType=='location')
				$sIdQuery = ' AND `location_id`='.$iEntryId;
			else
				$sIdQuery = ' AND `sub_id`='.$iEntryId;
		}

		if($sType)
			$sTypeQuery = " AND `type`='$sType'";
 
		$bCount = $this->getOne("SELECT COUNT(`member_id`) FROM `" . $this->_sPrefix . "review` WHERE 1  {$sTypeQuery} {$sIdQuery}");

		return $bCount;   
	}
  
	function postReview($iEntryId, $iSubEntryId, $iProfileId, $sType='location') { 

		 $iTime = time();
		 $sReview = process_db_input($_REQUEST['review_text']);
 
		 return $this->query("INSERT INTO `" . $this->_sPrefix . "review` SET `location_id`='$iEntryId', `sub_id`='$iSubEntryId', `member_id`='$iProfileId', `desc`='$sReview', `type`='$sType', `created`=$iTime"); 
	}
 
	function getAllFans($iEntryId, $sStatus='here', $sqlLimit='') {

		$aAllEntries = $this->getAll("SELECT `id_profile` FROM `" . $this->_sPrefix . "fans` WHERE `id_entry`='{$iEntryId}' AND `status`='$sStatus' ORDER BY `when` {$sqlLimit}");

		return $aAllEntries;   
	}

 	function getFanCount($iEntryId, $sStatus='here') {

		$bCount = $this->getOne("SELECT COUNT(`id_profile`) FROM `" . $this->_sPrefix . "fans` WHERE `id_entry`='{$iEntryId}' AND `status`='$sStatus'");

		return $bCount;   
	}

	function addFan($iEntryId, $iProfileId, $sStatus) { 
		 $iTime = time();

		 if($this->isFan($iEntryId, $iProfileId)) {
			return $this->query("UPDATE `" . $this->_sPrefix . "fans` SET `status`='$sStatus', `when`=$iTime WHERE `id_entry`='$iEntryId' AND `id_profile`='$iProfileId'");  
		 }else{
			return $this->query("INSERT INTO `" . $this->_sPrefix . "fans` SET `id_entry`='$iEntryId', `id_profile`='$iProfileId', `status`='$sStatus', `when`=$iTime");  
		 }
	}
 
	function isFan($iEntryId, $iProfileId, $bCheckStatus=false, $sStatus='') { 

		 $sStatusQuery = ($bCheckStatus) ? " AND `status`='$sStatus'" : "";

		 $bFan = $this->getOne("SELECT `id_profile`  FROM `" . $this->_sPrefix . "fans` WHERE `id_entry`='$iEntryId' AND `id_profile`='$iProfileId' {$sStatusQuery} LIMIT 1");  
	
		return $bFan;
	}


	/***** EVENT **************************************/
    function getEventEntryById($iId) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableEvent . "` WHERE `{$this->_sFieldId}` = $iId LIMIT 1");
    }
 
    function getEventEntryByUri($sUri) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableEvent . "` WHERE `{$this->_sFieldUri}` = '$sUri' LIMIT 1");
    }
 
    function deleteEventByIdAndOwner ($iId, $iLocationId, $iOwner, $isAdmin) {
 
        $sWhere = '';
        if (!$isAdmin) 
            $sWhere = " AND `{$this->_sFieldAuthorId}` = '$iOwner' ";
        if (!($iRet = $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableEvent . "` WHERE `{$this->_sFieldId}` = $iId AND `location_id`=$iLocationId $sWhere LIMIT 1")))
            return false;
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableEventMediaPrefix . "files` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableEventMediaPrefix . "images` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableEventMediaPrefix . "videos` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableEventMediaPrefix . "sounds` WHERE `entry_id` = $iId");
   
        return true;
    }   
 
	/***** FOOD **************************************/
    function getFoodEntryById($iId) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableFood . "` WHERE `{$this->_sFieldId}` = $iId LIMIT 1");
    }
 
    function getFoodEntryByUri($sUri) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableFood . "` WHERE `{$this->_sFieldUri}` = '$sUri' LIMIT 1");
    }
 
    function deleteFoodByIdAndOwner ($iId, $iLocationId,  $iOwner, $isAdmin) {
        $sWhere = '';
        if (!$isAdmin) 
            $sWhere = " AND `{$this->_sFieldAuthorId}` = '$iOwner' ";
        if (!($iRet = $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableFood . "` WHERE `{$this->_sFieldId}` = $iId AND `location_id`=$iLocationId $sWhere LIMIT 1")))
            return false;
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableFoodMediaPrefix . "files` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableFoodMediaPrefix . "images` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableFoodMediaPrefix . "videos` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableFoodMediaPrefix . "sounds` WHERE `entry_id` = $iId");
  
        return true;
    }   
 
	/***** HOTEL **************************************/
    function getHotelEntryById($iId) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableHotel . "` WHERE `{$this->_sFieldId}` = $iId LIMIT 1");
    }
 
    function getHotelEntryByUri($sUri) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableHotel . "` WHERE `{$this->_sFieldUri}` = '$sUri' LIMIT 1");
    }
 
    function deleteHotelByIdAndOwner ($iId, $iLocationId, $iOwner, $isAdmin) {
        $sWhere = '';
        if (!$isAdmin) 
            $sWhere = " AND `{$this->_sFieldAuthorId}` = '$iOwner' ";
        if (!($iRet = $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableHotel . "` WHERE `{$this->_sFieldId}` = $iId AND `location_id`=$iLocationId $sWhere LIMIT 1")))
            return false;
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableHotelMediaPrefix . "files` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableHotelMediaPrefix . "images` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableHotelMediaPrefix . "videos` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableHotelMediaPrefix . "sounds` WHERE `entry_id` = $iId");
  
        return true;
    }   
 

	/***** Nightlife **************************************/
    function getNightlifeEntryById($iId) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableNightlife . "` WHERE `{$this->_sFieldId}` = $iId LIMIT 1");
    }
 
    function getNightlifeEntryByUri($sUri) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableNightlife . "` WHERE `{$this->_sFieldUri}` = '$sUri' LIMIT 1");
    }
 
    function deleteNightlifeByIdAndOwner ($iId, $iLocationId, $iOwner, $isAdmin) {
        $sWhere = '';
        if (!$isAdmin) 
            $sWhere = " AND `{$this->_sFieldAuthorId}` = '$iOwner' ";
        if (!($iRet = $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableNightlife . "` WHERE `{$this->_sFieldId}` = $iId AND `location_id`=$iLocationId $sWhere LIMIT 1")))
            return false;
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableNightlifeMediaPrefix . "files` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableNightlifeMediaPrefix . "images` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableNightlifeMediaPrefix . "videos` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableNightlifeMediaPrefix . "sounds` WHERE `entry_id` = $iId");
   
        return true;
    }   
 

	/***** TRANSPORT **************************************/
    function getTransportEntryById($iId) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableTransport . "` WHERE `{$this->_sFieldId}` = $iId LIMIT 1");
    }
 
    function getTransportEntryByUri($sUri) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableTransport . "` WHERE `{$this->_sFieldUri}` = '$sUri' LIMIT 1");
    }
 
    function deleteTransportByIdAndOwner ($iId, $iLocationId, $iOwner, $isAdmin) {
        $sWhere = '';
        if (!$isAdmin) 
            $sWhere = " AND `{$this->_sFieldAuthorId}` = '$iOwner' ";
        if (!($iRet = $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableTransport . "` WHERE `{$this->_sFieldId}` = $iId AND `location_id`=$iLocationId $sWhere LIMIT 1")))
            return false;
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableTransportMediaPrefix . "files` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableTransportMediaPrefix . "images` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableTransportMediaPrefix . "videos` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableTransportMediaPrefix . "sounds` WHERE `entry_id` = $iId");
 
        return true;
    }   
 

	/***** SHOP **************************************/
    function getShopEntryById($iId) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableShop . "` WHERE `{$this->_sFieldId}` = $iId LIMIT 1");
    }
 
    function getShopEntryByUri($sUri) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableShop . "` WHERE `{$this->_sFieldUri}` = '$sUri' LIMIT 1");
    }
 
    function deleteShopByIdAndOwner ($iId, $iLocationId, $iOwner, $isAdmin) {
        $sWhere = '';
        if (!$isAdmin) 
            $sWhere = " AND `{$this->_sFieldAuthorId}` = '$iOwner' ";
        if (!($iRet = $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableShop . "` WHERE `{$this->_sFieldId}` = $iId AND `location_id`=$iLocationId $sWhere LIMIT 1")))
            return false;
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableShopMediaPrefix . "files` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableShopMediaPrefix . "images` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableShopMediaPrefix . "videos` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableShopMediaPrefix . "sounds` WHERE `entry_id` = $iId");
 
        return true;
    }   
 

	/***** SERVICE **************************************/
    function getServiceEntryById($iId) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableService . "` WHERE `{$this->_sFieldId}` = $iId LIMIT 1");
    }
 
    function getServiceEntryByUri($sUri) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableService . "` WHERE `{$this->_sFieldUri}` = '$sUri' LIMIT 1");
    }
 
    function deleteServiceByIdAndOwner ($iId, $iLocationId, $iOwner, $isAdmin) {
        $sWhere = '';
        if (!$isAdmin) 
            $sWhere = " AND `{$this->_sFieldAuthorId}` = '$iOwner' ";
        if (!($iRet = $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableService . "` WHERE `{$this->_sFieldId}` = $iId AND `location_id`=$iLocationId $sWhere LIMIT 1")))
            return false;
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableServiceMediaPrefix . "files` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableServiceMediaPrefix . "images` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableServiceMediaPrefix . "videos` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableServiceMediaPrefix . "sounds` WHERE `entry_id` = $iId");
 
        return true;
    }   
 

	/***** SCHOOL **************************************/
    function getSchoolEntryById($iId) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableSchool . "` WHERE `{$this->_sFieldId}` = $iId LIMIT 1");
    }
 
    function getSchoolEntryByUri($sUri) {
         return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableSchool . "` WHERE `{$this->_sFieldUri}` = '$sUri' LIMIT 1");
    }
 
    function deleteSchoolByIdAndOwner ($iId, $iLocationId, $iOwner, $isAdmin) {
        $sWhere = '';
        if (!$isAdmin) 
            $sWhere = " AND `{$this->_sFieldAuthorId}` = '$iOwner' ";
        if (!($iRet = $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableSchool . "` WHERE `{$this->_sFieldId}` = $iId AND `location_id`=$iLocationId $sWhere LIMIT 1")))
            return false;
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableSchoolMediaPrefix . "files` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableSchoolMediaPrefix . "images` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableSchoolMediaPrefix . "videos` WHERE `entry_id` = $iId");
        $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableSchoolMediaPrefix . "sounds` WHERE `entry_id` = $iId");
 
        return true;
    }   
    
    function getLocalMembers(&$aProfiles, $iProfileId, $iStart, $iMaxNum ) {
  
		$aProfileInfo = getProfileInfo($iProfileId);
		$sCountry = $aProfileInfo['Country'];
		$sCity = strtolower($aProfileInfo['City']);
		$sCity = process_db_input($sCity);

        $aProfiles = $this->getAll ("SELECT SQL_CALC_FOUND_ROWS `p`.* FROM `Profiles` AS `p`  WHERE `p`.`Country`='$sCountry' AND lower(`p`.`City`)='$sCity' AND `p`.`Status` = 'Active' AND `ID` != $iProfileId  ORDER BY `p`.`DateReg` DESC LIMIT $iStart, $iMaxNum");
        return $this->getOne("SELECT FOUND_ROWS()");
    }
 
	//begin Deals
	function processDeals(){
		$iTime = time();
  		
		$this->query("DELETE FROM `" . $this->_sPrefix . "deals` WHERE `end_date` < {$iTime}");  
 	}
 
	function getDeals($iEntryId, $sType){
		$iTime = time();
		$iEntryId = (int)$iEntryId;

		return $this->getAll("SELECT `id`, `id_entry`, `name`, `count`, `start_date`, `end_date`, `price` FROM `" . $this->_sPrefix . "deals` WHERE `type`='$sType' AND `id_entry`=$iEntryId AND `end_date`>{$iTime}");
	}

  	function getDeal($iEntryId, $sType, $iLimit=0){
		$iEntryId = (int)$iEntryId;

		if($iLimit)
			$sQuery = "LIMIT 0, {$iLimit}";

		return $this->getAll("SELECT `id`, `id_entry`, `name`, `count`, `start_date`, `end_date`, `price` FROM `" . $this->_sPrefix . "deals` WHERE `type`='$sType' AND `id_entry`={$iEntryId} {$sQuery}");
	}

    function _passDate ($iYear, $iMonth, $iDay) {
         $iDay   = (int)$iDay;
        $iMonth = (int)$iMonth;
        $iYear  = (int)$iYear;
        $iRet = mktime (0, 0, 0, $iMonth, $iDay, $iYear);
        return $iRet > 0 ? $iRet : 0;
    }

 	function addDeal($iEntryId, $sType){

		foreach($_POST['dealcaption'] as $iKey=>$sValue){
		
			$sDealName = process_db_input($sValue);
 			$iCount = process_db_input($_POST['count'][$iKey]);
  			
			$iStartDay = (int)process_db_input($_POST['start_date_day'][$iKey]);
			$iStartMonth = (int)process_db_input($_POST['start_date_month'][$iKey]);
  			$iStartYear = (int)process_db_input($_POST['start_date_yr'][$iKey]);
			$iStartDate = (int)$this->_passDate ($iStartYear, $iStartMonth, $iStartDay);
			
			$iEndDay = (int)process_db_input($_POST['end_date_day'][$iKey]);
			$iEndMonth = (int)process_db_input($_POST['end_date_month'][$iKey]);
  			$iEndYear = (int)process_db_input($_POST['end_date_yr'][$iKey]);
			$iEndDate = (int)$this->_passDate ($iEndYear, $iEndMonth, $iEndDay);
 			
			$iPrice = process_db_input($_POST['price'][$iKey]);
  
			if(trim($sDealName)){  
				$this->query("INSERT INTO `" . $this->_sPrefix . "deals` SET `type`='$sType', `id_entry`=$iEntryId, `count`='$iCount', `name`='$sDealName',  `price`='$iPrice', `start_date`='$iStartDate', `end_date`='$iEndDate'");
			}
		} 
	}

	function removeEntryDeal($iEntryId, $sType){ 
	   
		$this->query("DELETE FROM `" . $this->_sPrefix . "deals` WHERE `type`='$sType' AND `id_entry`='$iEntryId'");   
	}

	function removeDeal($iEntryId, $iFeedId, $sType){ 
	   
		$this->query("DELETE FROM `" . $this->_sPrefix . "deals` WHERE `type`='$sType' AND `id_entry`='$iEntryId' AND `id`='$iFeedId'");   
	}

    function getDealIds ($iEntryId, $sType) {
		return $this->getPairs ("SELECT `id` FROM `" . $this->_sPrefix . "deals`  WHERE `type`='$sType' AND `id_entry` = '$iEntryId'", 'id', 'id');
    }
	//END Deals

    function getSubProfileEntryById($sType, $iId) {

		switch($sType){
			case 'nightlife': 
				$sTable = $this->_sTableNightlife;
			break;
			case 'shop': 
				$sTable = $this->_sTableShop;
			break;
			case 'school': 
				$sTable = $this->_sTableSchool;
			break;
			case 'service': 
				$sTable = $this->_sTableService;
			break;
			case 'hotel': 
				$sTable = $this->_sTableHotel;
			break;
			case 'event': 
				$sTable = $this->_sTableEvent;
			break;
			case 'transport': 
				$sTable = $this->_sTableTransport;
			break;
			case 'food': 
				$sTable = $this->_sTableFood;
			break;
		}
  
        return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $sTable . "` WHERE `{$this->_sFieldId}` = $iId LIMIT 1");
    }


    function insertSubProfileLocation ($sSubType, $iAuthorId, $iId, $fLat, $fLng, $iMapZoom, $sMapType, $sAddress, $sCountry, $iPrivacy = 0, $isFailed = 0, $sPrefix='') {
        $sPrivacyUpdate = '';
        $sPrivacyInsert = "`allow_view_location_to` = '" . BX_AUTO_DEFAULT_PRIVACY . "',";
        if ($iPrivacy) {
            $sPrivacyInsert = $sPrivacyUpdate = "`allow_view_location_to` = '$iPrivacy',";
        }  
 
        return $this->query ("INSERT INTO `" . $this->_sPrefix . "sub_profiles` SET `id` = '$iId', `author_id` = '$iAuthorId', `lat` = '$fLat', `lng` = '$fLng', `zoom` = '$iMapZoom', `subtype` = '$sSubType', `type` = '$sMapType', `address` = '$sAddress', `country`= '$sCountry', $sPrivacyInsert `ts` = UNIX_TIMESTAMP(), `failed` = '$isFailed' ON DUPLICATE KEY UPDATE  `lat` = '$fLat', `lng` = '$fLng', `zoom` = '$iMapZoom', `type` = '$sMapType', `address` = '$sAddress', `country`= '$sCountry', $sPrivacyUpdate `ts` = UNIX_TIMESTAMP(), `failed` = '$isFailed'");
    }

    function getSubProfileById($sSubType, $iProfileId) { 
 
		switch($sSubType){
			case 'nightlife': 
				$sTable = $this->_sTableNightlife;
			break;
			case 'shop': 
				$sTable = $this->_sTableShop;
			break;
			case 'school': 
				$sTable = $this->_sTableSchool;
			break;
			case 'service': 
				$sTable = $this->_sTableService;
			break;
			case 'hotel': 
				$sTable = $this->_sTableHotel;
			break;
			case 'event': 
				$sTable = $this->_sTableEvent;
			break;
			case 'transport': 
				$sTable = $this->_sTableTransport;
			break;
			case 'food': 
				$sTable = $this->_sTableFood;
			break;
		}
 
        return $this->getRow("SELECT `m`.`id`, `p`.`title`, `p`.`address1`, `p`.`address2`, `p`.`thumb`, `m`.`lat`, `m`.`lng`, `m`.`zoom`, `m`.`type`, `m`.`address`, `m`.`country`, `p`.`city`, `m`.`allow_view_location_to` FROM `" . $this->_sPrefix . "sub_profiles` AS `m` INNER JOIN `" . $this->_sPrefix . $sTable . "` AS `p` ON (`p`.`id` = `m`.`id`) WHERE `m`.`failed` = 0 AND `p`.`status` = 'approved' AND `m`.`id` = '$iProfileId' AND `subtype` = '$sSubType' LIMIT 1");
    } 
 
    function getLocationIdentifier($sType) { 
 
		switch($sType){
			case 'nightlife': 
				$sIdentifier = _t('_modzzz_location_nightlife');
			break;
			case 'shop': 
				$sIdentifier = _t('_modzzz_location_shop');
			break;
			case 'school': 
				$sIdentifier = _t('_modzzz_location_school');
			break;
			case 'service': 
				$sIdentifier = _t('_modzzz_location_service');
			break;
			case 'hotel': 
				$sIdentifier = _t('_modzzz_location_hotel');
			break;
			case 'event': 
				$sIdentifier = _t('_modzzz_location_event');
			break;
			case 'transport': 
				$sIdentifier = _t('_modzzz_location_transport');
			break;
			case 'food': 
				$sIdentifier = _t('_modzzz_location_food');
			break;
			default:
				$sIdentifier = _t('_modzzz_location'); 
		}

		return $sIdentifier; 
	}



}

?>